//
//  UIPicCollectionCell.m
//  Project_B
//
//  Created by dllo on 15/12/23.
//  Copyright © 2015年 ZY. All rights reserved.
//

#import "LHPicCollectionCell.h"

@implementation LHPicCollectionCell
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubViews];
    }
    return self;
}
- (void)createSubViews
{
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    //row高度
    _tableView.rowHeight = 265;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //注册cell
    [_tableView registerClass:[LHPictureCell class] forCellReuseIdentifier:@"LHPictureCell"];
    [_tableView registerClass:[LHPictureTwoCell class] forCellReuseIdentifier:@"LHPictureTwoCell"];
    [_tableView registerClass:[LHPictureThreeCell class] forCellReuseIdentifier:@"LHPictureThreeCell"];
    [self.contentView addSubview:_tableView];
    self.dataArray = [NSMutableArray array];
    self.isUp = NO;
    [self addHeaderRefresh];
    [self addFooterRefresh];
}

#pragma mark ** 下拉刷新
-(void)addHeaderRefresh
{
    
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.isUp = NO;
        [self getDataID:@"list" setid:@""];
    }];
    
    [_tableView.mj_header beginRefreshing];
}

#pragma mark ** 上拉加载
-(void)addFooterRefresh
{
    _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.isUp = YES;
        PictureModel *model = [self.dataArray lastObject];
        NSString *str = [NSString stringWithFormat:@"/%@", model.setid];
        [self getDataID:@"morelist" setid:str];
    }];
}


#pragma mark ** 获取数据
- (void)getDataID:(NSString *)string setid:(NSString *)setid {
    
    NSString *url = [NSString stringWithFormat:@"http://c.m.163.com/photo/api/%@/0096/4GJ60096%@.json", string, setid];
    
    [LHAFNTools appToolsWithSourcePath:url block:^(id result) {
        if (self.isUp == NO) {
            [self.dataArray removeAllObjects];
        }
        for (NSMutableDictionary *dic in result) {
            PictureModel *model = [[PictureModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            if (model.pics.count >= 3) {
                [self.dataArray addObject:model];
            }
        }
        [_tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return _dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.item % 3 == 0) {
        LHPictureCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LHPictureCell" forIndexPath:indexPath];
        cell.model = [_dataArray objectAtIndex:indexPath.item];
        return cell;
    }else if (indexPath.item % 3 == 1){
        LHPictureTwoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LHPictureTwoCell" forIndexPath:indexPath];
        cell.model = [_dataArray objectAtIndex:indexPath.item];
        return cell;
    }else{
        LHPictureThreeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LHPictureThreeCell" forIndexPath:indexPath];
        cell.model = [_dataArray objectAtIndex:indexPath.item];
        return cell;
    }

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.setname = [[_dataArray objectAtIndex:indexPath.row] setname];
    self.setid = [[_dataArray objectAtIndex:indexPath.row] setid];
    [self.LHPicDelegate pushAction];
    
}


@end
